//分页插件

(function($){
	var ms = {
		init:function(obj,args){
			return (function(){
				ms.fillHtml(obj,args);
				ms.bindEvent(obj,args);
			})();
		},
		//填充html
		fillHtml:function(obj,args){
			return (function(){
			    obj.empty();
			    //---------上一页
				if(args.current > 1){
				    obj.append('<div class="page1"><a href="javascript:;" class="btn prevPage">上一页</a></div> ');
				}else{
					obj.remove('.prevPage');
					obj.append('<div class="page1"><a href="javascript:void(0);" class="btn" style="background: #ddd !important;">上一页</a></div>');
				}

			    //---------中间页码
				obj.append('<div class="page2 text-center"><span>总共&nbsp;<em class="pageCount">' + args.pageCount + '</em>&nbsp;条 / 当前第&nbsp;<em class="current">' + args.current + '</em>&nbsp;页</span></div>');

			    //---------下一页
				if (args.current < (args.pageCount / args.pageSize)) {
				    obj.append('<div class="page3"><a href="javascript:;" class="btn nextPage">下一页</a></div>');
				}else{
					obj.remove('.nextPage');
					obj.append('<div class="page3"><a href="javascript:void(0);" class="btn" style="background: #ddd !important;">下一页</a></div>');
				}
			})();
		},
		//绑定事件
		bindEvent:function(obj,args){
			return (function(){
				//obj.on("click","a.tcdNumber",function(){
				//	var current = parseInt($(this).text());
				//	ms.fillHtml(obj,{"current":current,"pageCount":args.pageCount});
				//	if(typeof(args.backFn)=="function"){
				//		args.backFn(current);
				//	}
			    //});
				//上一页
				obj.on("click","a.prevPage",function(){
				    var current = parseInt($(".current").text());
				    ms.fillHtml(obj, { "current": current - 1, "pageCount": args.pageCount, "pageSize": args.pageSize });
					if(typeof(args.backFn)=="function"){
						args.backFn(current-1);
					}
				});
				//下一页
				obj.on("click", "a.nextPage", function () {
					var current = parseInt($(".current").text());
					ms.fillHtml(obj, { "current": current + 1, "pageCount": args.pageCount, "pageSize": args.pageSize });
					if(typeof(args.backFn)=="function"){
						args.backFn(current+1);
					}
				});
			})();
		}
	}
	$.fn.createPage = function(options){
		var args = $.extend({
		    pageCount: 1,
		    pageSize: 10,
			current : 1,
			backFn : function(){}
		},options);
		ms.init(this,args);
	}
})(jQuery);
